package cn.enilu.flash.api.controller.pro;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.entity.pro.Community;
import cn.enilu.flash.service.pro.CommunityService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.factory.Page;


import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/community")
public class CommunityController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private CommunityService communityService;

	@GetMapping(value = "/list")
	@RequiresPermissions(value = "/community")
	public Object list(@RequestParam(required = false) Long id) {
		Page<Community> page = new PageFactory<Community>().defaultPage();
		page.addFilter("id",id);
		page = communityService.queryPage(page);
		return Rets.success(page);
	}
	@PostMapping
	@BussinessLog(value = "新增社区", key = "name")
	@RequiresPermissions(value = "/community/add")
	public Object add(@ModelAttribute Community community){
		communityService.insert(community);
		return Rets.success();
	}
	@PutMapping
	@BussinessLog(value = "更新社区", key = "name")
	@RequiresPermissions(value = "/community/update")
	public Object update(@ModelAttribute Community community){
		communityService.update(community);
		return Rets.success();
	}
	@DeleteMapping
	@BussinessLog(value = "删除社区", key = "id")
	@RequiresPermissions(value = "/community/delete")
	public Object remove(Long id){
		if (id == null) {
			throw new ApplicationException(BizExceptionEnum.REQUEST_NULL);
		}
		communityService.delete(id);
		return Rets.success();
	}
}